package pl.edu.agh.uddiProxy.parameter;

import org.apache.log4j.Logger;

import pl.edu.agh.uddiProxy.dao.DAOHelper;
import pl.edu.agh.uddiProxy.dao.DynamicParameterDAO;
import pl.edu.agh.uddiProxy.types.ParameterValue;
import pl.edu.agh.uddiProxy.types.ServiceDynamicParameters;

public class DownloadWSDLParameter {
	private static final String PARAMETER_NAME = "downloadWSDL";

	private static Logger logger = Logger.getLogger(DownloadWSDLParameter.class);
	
	public static synchronized void update(String tModelKey) {
		if (DAOHelper.getParametersMap().getParametersMap().containsKey(PARAMETER_NAME)) {
			DynamicParameterDAO parameterDAO = DAOHelper.getDynamicParameterDAO();
			
			ServiceDynamicParameters dynamicParameters;
			if (parameterDAO.exists(tModelKey)) {
				dynamicParameters = parameterDAO.getByTModel(tModelKey);
			} else {
				dynamicParameters = new ServiceDynamicParameters(tModelKey);
			}
			ParameterValue value = dynamicParameters.getParameterValue(PARAMETER_NAME);
			value.incrementNumberOfMeasurements();
			long v = value.getValue() == null ? 0 : Long.parseLong(value.getValue());
			value.setValue(Long.toString(++v));
			
			logger.info(tModelKey + " " + v);
			
			if (DAOHelper.getDynamicParameterDAO().exists(tModelKey)) {
				DAOHelper.getDynamicParameterDAO().update(dynamicParameters);
			} else {
				DAOHelper.getDynamicParameterDAO().create(dynamicParameters);	
			}
		}
			
	}
}
